package org.jeecg.modules.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.SpringContextUtils;
import org.springframework.stereotype.Component;

/**
 * @program: simulator-b20240918
 * @description:
 * @author: renzhg
 * @create: 2024-10-28 11:13
 **/
@Slf4j
@Component
public class MQTTUtils {

    /**
     * 消息发送
     *
     * @param message 消息
     */
    public static void send(String message) {
        try {
            MQTTGateway mqttGateway = SpringContextUtils.getBean(MQTTGateway.class);
            mqttGateway.sendToMqtt(message);
        } catch (Exception ex) {
            log.error("发送MQTT消息失败：" + ex.getMessage());
        }

    }

    /**
     * 指定topic进行消息发送
     *
     * @param topic   主题
     * @param message 消息
     */
    public static void send(String topic, String message) {
        try {
            MQTTGateway mqttGateway = SpringContextUtils.getBean(MQTTGateway.class);
            mqttGateway.sendToMqtt(topic, message);
        } catch (Exception ex) {
            log.error("发送MQTT消息失败：" + ex.getMessage());
        }
    }

    /**
     * 指定topic和qos进行消息发送
     *
     * @param topic   主题
     * @param qos     qos
     * @param message 消息
     */
    public static void send(String topic, int qos, String message) {
        try {
            MQTTGateway mqttGateway = SpringContextUtils.getBean(MQTTGateway.class);
            mqttGateway.sendToMqtt(topic, qos, message);
        } catch (Exception ex) {
            log.error("发送MQTT消息失败：" + ex.getMessage());
        }
    }

    /**
     * 指定topic和qos进行消息发送
     *
     * @param topic   主题
     * @param qos     qos
     * @param message 消息(字节数字类型)
     */
    public static void send(String topic, int qos, byte[] message) {
        try {
            MQTTGateway mqttGateway = SpringContextUtils.getBean(MQTTGateway.class);
            mqttGateway.sendToMqtt(topic, qos, message);
        } catch (Exception ex) {
            log.error("发送MQTT消息失败：" + ex.getMessage());
        }
    }
}
